{% extends "base.html" %}

{% block content %}
<div class="container mt-5">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>论文列表</h2>
        <a href="{{ url_for('main.submit_thesis') }}" class="btn btn-primary">
            <i class="fas fa-plus"></i> 新建论文
        </a>
    </div>
    <div class="row">
        {% for thesis in theses %}
        <div class="col-md-6 mb-4">
            <div class="card h-100">
                <div class="card-body">
                    <h5 class="card-title">{{ thesis.title }}</h5>
                    <p class="card-text">{{ thesis.description[:100] }}{% if thesis.description|length > 100 %}...{% endif %}</p>
                    <ul class="list-unstyled">
                        <li><strong>编程语言：</strong> {{ thesis.programming_language }}</li>
                        <li><strong>预算：</strong> 
                            ¥{{ "%.2f"|format(thesis.price) }}
                            {% if thesis.need_environment %}
                            <small class="text-muted">（+环境配置100元）</small>
                            {% endif %}
                        </li>
                        <li>
                            <strong>状态：</strong>
                            {% if thesis.status == 'pending' %}
                                <span class="badge bg-warning">待处理</span>
                            {% elif thesis.status == 'accepted' %}
                                <span class="badge bg-info">已接受</span>
                            {% elif thesis.status == 'completed' %}
                                <span class="badge bg-success">已完成</span>
                            {% endif %}
                        </li>
                    </ul>
                </div>
                <div class="card-footer">
                    <div class="d-flex justify-content-between align-items-center">
                        <small class="text-muted">{{ thesis.created_at.strftime('%Y-%m-%d') }}</small>
                        <div class="btn-group">
                            <a href="{{ url_for('main.thesis_detail', id=thesis.id, source='user') }}" 
                               class="btn btn-primary btn-sm">
                                详情
                            </a>
                            {% if current_user.id == thesis.user_id %}
                            <a href="{{ url_for('main.edit_thesis', id=thesis.id) }}" class="btn btn-warning btn-sm">编辑</a>
                            <button type="button" class="btn btn-danger btn-sm" onclick="deleteThesis({{ thesis.id }})">删除</button>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>
        {% else %}
        <div class="col-12">
            <div class="alert alert-info">
                还没有提交过论文，点击右上角"新建论文"开始提交吧！
            </div>
        </div>
        {% endfor %}
    </div>
    
    {% if pagination and pagination.pages > 1 %}
    <nav aria-label="Page navigation" class="mt-4">
        <ul class="pagination justify-content-center">
            {% for page in pagination.iter_pages() %}
                {% if page %}
                    <li class="page-item {% if page == pagination.page %}active{% endif %}">
                        <a class="page-link" href="{{ url_for('main.my_theses', page=page) }}">{{ page }}</a>
                    </li>
                {% else %}
                    <li class="page-item disabled">
                        <span class="page-link">...</span>
                    </li>
                {% endif %}
            {% endfor %}
        </ul>
    </nav>
    {% endif %}
</div>

<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">确认删除</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                确定要删除这个论文吗？
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="deleteForm" method="POST" class="d-inline">
                    <button type="submit" class="btn btn-danger">确定删除</button>
                </form>
            </div>
        </div>
    </div>
</div>

{% block scripts %}
<script>
function deleteThesis(id) {
    const deleteForm = document.getElementById('deleteForm');
    deleteForm.action = "{{ url_for('main.delete_thesis', id=0) }}".replace('0', id);
    new bootstrap.Modal(document.getElementById('deleteModal')).show();
}

document.addEventListener('DOMContentLoaded', function() {
    const images = document.querySelectorAll('img[data-src]');
    images.forEach(img => {
        img.src = img.dataset.src;
    });
});
</script>
{% endblock %}
{% endblock %} 